<?php
require APPPATH.'libraries/ApiController.php';
class paytype extends ApiController {
	public function __construct()
	{
		parent::__construct();
	}
	
	public function get_paytype_list(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
				["uid",true,"会员ID"]],'post');
			$this->db->where("shopid",$token["shopid"]);
			$pids=$this->db->select("paytypes")->get("shop")->row_array()["paytypes"];
			$list=[];
			if($pids!=""){
				$pidarr=explode(",",$pids);
				if($data["uid"]==-1){
					foreach($pidarr as $key=>$value){
						if((int)$value===8){
							unset($pidarr[$key]);
						}
						if((int)$value===2){
							unset($pidarr[$key]);
						}
						if((int)$value===6){
							unset($pidarr[$key]);
						}
					}
				}
				
				$this->db->select("ptid,pttitle,ptname,concat('".$this->config->item("domain")."',icon) as icon");
				$list=$this->db->where_in("ptid",$pidarr)->get("paytype")->result_array();
			}
 
			$this->WRITE("code",1);
			$this->WRITE("msg","操作成功!");	
			$this->WRITE("list",$list);
			$this->RESPONSE();
		}
	}
	
	
	//更新店铺支付方式
	public function update_shop_paytype(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
				["paytypes",false,"收款数据",""]],'post');
			
			$this->db->where("shopid",$token["shopid"])->update("shop",array("paytypes"=>$data["paytypes"]));
			$this->WRITE("code",1);
			$this->WRITE("msg","操作成功!");	
			$this->RESPONSE();
		}
	}
	
	
	public function get_shop_paytypes(){
		$token=$this->verifyToken();
		if($token){
			$this->db->where("shopid",$token["shopid"]);
			$pids=$this->db->select("paytypes")->get("shop")->row_array()["paytypes"];
			$pidarr=$pids==""? []:explode(",",$pids);
			$this->db->select("ptid,pttitle,'false' as status,ptname,concat('".$this->config->item("domain")."',icon) as icon");
			$list=$this->db->get("paytype")->result_array();
			
			for($i=0;$i<count($list);$i++){
				$list[$i]["status"]=$this->findInArray($pidarr,$list[$i]["ptid"]);
			}
			
			
			/*$list=[];
			if($pids!=""){
				$pidarr=explode(",",$pids);
				$this->db->select("ptid,pttitle,ptname,0 as rmb,concat('".$this->config->item("domain")."',icon) as icon");
				$list=$this->db->->get("paytype")->result_array();
			}*/
			$this->WRITE("code",1);
			$this->WRITE("msg","操作成功!");	
			$this->WRITE("list",$list);
			$this->RESPONSE();
		}
		
	}
	private function findInArray($arr,$val){
		foreach($arr as $v){
			if($v===$val){
				return true;
			}
		}
		return false;
	}
	
	
	
	public function get_multipay_list(){
		$token=$this->verifyToken();
		if($token){
			$this->db->where("shopid",$token["shopid"]);
			$pids=$this->db->select("paytypes")->get("shop")->row_array()["paytypes"];
			$list=[];
			if($pids!=""){
				$pidarr=explode(",",$pids);

				foreach($pidarr as $key=>$value){
					if((int)$value===8){
						unset($pidarr[$key]);
					}
				}
				$this->db->select("ptid,pttitle,ptname,0 as rmb,concat('".$this->config->item("domain")."',icon) as icon");
				$list=$this->db->where_in("ptid",$pidarr)->get("paytype")->result_array();
			}

			$this->WRITE("code",1);
			$this->WRITE("msg","操作成功!");	
			$this->WRITE("list",$list);
			$this->RESPONSE();
		}
	}
	
	public function get_charge_types(){
		$token=$this->verifyToken();
		if($token){
			$pidarr=array(1,3,4,5,7);
			$this->db->select("ptid,pttitle,ptname,0 as rmb,concat('".$this->config->item("domain")."',icon) as icon");
			$list=$this->db->where_in("ptid",$pidarr)->get("paytype")->result_array();
			$this->WRITE("code",1);
			$this->WRITE("msg","操作成功!");	
			$this->WRITE("list",$list);
			$this->RESPONSE();
		}
	}
}